<?php 
/*
 * 列表页
 * @arrange Sameal
 * @package ixdcw
 * @file    list.inc.php
 * @time    13-3-14 下午1:15
 */
defined('IN_IXDCW') or exit('Access Denied');

require DT_CORE.'/'.$module.'/common.inc.php';
// 检测类别是否和模块符合
if(!empty($CAT) && $CAT['moduleid'] != $moduleid) {
	$head_title = lang('message->cate_not_exists');
	@header("HTTP/1.1 404 Not Found");
	exit(include template('list-notfound', 'message'));
}

if(!check_group($_groupid, $MOD['group_list']) || !check_group($_groupid, $CAT['group_list'])) {
	$head_title = lang('message->without_permission');
	exit(include template('noright', 'message'));
}

$group=PCache::read("grouporder.php");//读取缓存会员组排序
if(!$group){
	Xd_save_groupcachefile();
}

ViewCheck::catExists() or Prompt::listNotFound();
ViewCheck::groupListPermission() or Prompt::noPermission();

// normal
$filter['catid']  = getgpc('catid');
$filter['areaid'] = getgpc('areaid');
$filter['typeid'] = getgpc('typeid');

// auto
$filter['ctp'] = getgpc('ctp');
$filter['bid'] = getgpc('bid');
$filter['cid'] = getgpc('cid');
$filter['mid'] = getgpc('mid');
$filter['cen'] = getgpc('cen');




// 增加url自动生成
$buildBase = array('catid', 'areaid', 'typeid', 'order', 'kw', 'ctp', 'bid', 'cid', 'mid', 'page','cen');
$buildPreserve = 'typeid';
$buildDrop = 'page,kw';
$selectedAttrs = array();


$typeid = isset($typeid) && $typeid != null ? intval($typeid) : 99;
$order  = isset($order) ? intval($order) : 0;
$TYPES = explode('|', $MOD['type']);

require_once DT_INC . '/search/listAuto.inc.php';

$maincat = Search::mainCat(MODULE_NAME, $catid);

if (!empty($ARE)) {
    $mainarea=Xd_get_arealists($ARE);
} else {
    $mainarea[0] = get_mainarea(0);
}


$areaParentid = $ARE['parentid'];
if ($kw) {
    SearchCheck::length($kw);
    SearchCheck::timeLimit();
}require_once DT_INC.'/post.func.php';

include load('search.lang');

// 地区搜索.
if ($areaid) {
    $condition .=  $ARE['child'] ? " AND areaid IN (".$ARE['arrchildid'].") " : " AND areaid=$areaid ";
}
if ($kw) {
    $condition .= " AND title LIKE '%$kw%'";
}
if (!empty($CAT)) {
    $condition .= ($CAT['child']) ? " AND catid IN (" . $CAT['arrchildid'] . ")" : " AND catid=$catid ";
}
if ($typeid != 99 && array_key_exists($typeid, $TYPES)) {
    $condition .= " AND typeid=$typeid ";
}
if($cityid&&$cityid!=0){
	$city_order='(case when areaid IN ('.$AREA[$cityid]["arrchildid"].') then 1 else 2 end ),';
	$push_city_order='(case when city like "%|'.$cityid.'|%" or city like "%|0|%" then 1 else 2 end ),';
}else{
	$city_order='';
}


$dorder  = array( $MOD['order'],/* $group['DESC'], $group['ASC'], */'addtime DESC'/*, 'addtime ASC'*/);
$order=isset( $order ) ? intval($order) : 0;
isset($order) && isset($dorder[$order]) or $order = 0;


$tags = array();

if($kw) {
	Search::checkKwLength($kw);
	Search::checkTimeLimit();
}
$fds = $MOD['fields'];
$condition = "(status=3 or status=2)";


if ($ctp) {
	$condition .= " and arbid in (" . implode(',',array_keys($BRANDS)) . ")";
}
if ($bid) {
	$condition .= " and arbid in (" . $bid . ")";
}

if ($cid) {
	$condition .= " and arcid in (".$cid.")";
}

if ($mid) {
	$condition .= " and amid in (" .$mid. ")";
}

if($keyword) {
	$condition .= " AND keyword LIKE '%$keyword%'";
}
if (!empty($CAT)) {
	$condition .= ($CAT['child']) ? " AND catid IN (".$CAT['arrchildid'].")" : " AND {$table}.catid=$catid";
}
if($areaid) {
	$condition .= ($ARE['child']) ? " AND areaid IN (".$ARE['arrchildid'].")" : " AND areaid=$areaid";
}

$items = $db->count($table, $condition, $DT['cache_search']);

$do = new SellModule($moduleid);
$tags=array();
if($items) {
	$tags=$do->getList($condition,$push_city_order.$dorder[$order],($DT['cache_search'] && $page == 1) ? 'CACHE' : '', $DT['cache_search']);
	if($page == 1 && $kw) keyword($kw, $items, $moduleid);

}

//是否在线
foreach($tags as $v=>$k ){
    $username=$k;
    $user = userinfo($username['username']);
    $online = online($user['userid']);
    $tags[$v]['online']=$online;
}

$showpage = 1;
$datetype = 5;
//over


$seo_file = 'list';
include DT_INC.'/seo.inc.php';

$template = $CAT['template'] ? $CAT['template'] : 'list';


include template($template, $module);